package cn.itcast.utils;

import cn.itcast.mapper.UserMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

public class MapperUtils {
    private static Object mapper = null;
    private static SqlSession sqlSession = null;

    public static Object getMapper(Class<?> className) {
        try {
        InputStream is = Resources.getResourceAsStream("sqlMapper.xml");
        SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);
        sqlSession = factory.openSession(true);
        mapper = sqlSession.getMapper(className);
    } catch (IOException e) {
        e.printStackTrace();
    }
        return mapper;
    }
    public static void close() {
        if (sqlSession != null) {
            sqlSession.close();
        }
    }
}
